//
//  TongLanViewController.m
//  Article
//
//  Created by LONG on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "TongLanViewController.h"
#import "OllTableViewCell.h"
#import "AddMaterialViewController.h"
#import "MateModel.h"

@interface TongLanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *dataarr;

@end

@implementation TongLanViewController{
    __block int page;
}


- (UITableView *)myTableView{
    if (nil == _myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-143)];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [_myTableView registerNib:[UINib nibWithNibName:@"OllTableViewCell" bundle:nil] forCellReuseIdentifier:@"OllTableViewCell"];
        _myTableView.showsVerticalScrollIndicator = NO;
        [_myTableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(downMJ)];
        
        [self setExtraCellLineHidden:_myTableView];
        
        
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //[self requestTableview];
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.navigationBar.translucent = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];

    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addview)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    [self myTableView];
    
}
-(void)addview{
    AddMaterialViewController *avc = [[AddMaterialViewController alloc]init];
    avc.pushint = 1;
    [self.navigationController pushViewController:avc animated:YES];
}
//下拉刷新
-(void)downMJ{
    page = 1;
    [self requestTableview];
    
    [_myTableView.header endRefreshing];
}
//上拉加载
- (void)loadMoreData{
    page++;
    [self requestTableview];
    [_myTableView.footer endRefreshing];
    
}
- (void)requestTableview{
    if (_dataarr == nil || page == 1) {
        _dataarr = [NSMutableArray arrayWithCapacity:0];;
        
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%d",page],
                          @"type":@"banner",
                          @"user_token":[user objectForKey:@"token"]};
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:ADSLIST_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            //建立模型
            for (NSDictionary*dic in [responseObject objectForKey:@"data"]) {
                MateModel*model=[[MateModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                //追加数据
                [self.dataarr addObject:model];
            }
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        if (_dataarr.count > 8) {
            [_myTableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        }
        [self.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark tableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OllTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataarr.count == 0) {
        return cell;
    }
    MateModel*model=self.dataarr[indexPath.row];
    [cell configModel:model];
    cell.imagebut.hidden = YES;
    
    return cell;
}

#pragma mark delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*WIDTH/320;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddMaterialViewController *avc = [[AddMaterialViewController alloc]init];
    MateModel*model=self.dataarr[indexPath.row];
    avc.ads_id = model.id;
    avc.setTitle = @"通栏";
    avc.dataDic = [NSMutableDictionary new];
    [avc.dataDic setValue:model.ad_img forKey:@"ad_img"];
    [avc.dataDic setValue:model.ad_link forKey:@"ad_link"];
    
    [self.navigationController pushViewController:avc animated:YES];
    
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self downMJ];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;

}

@end
